package com.wangqinlei.cowims126.controller;

import com.wangqinlei.cowims126.pojo.Category;
import com.wangqinlei.cowims126.service.CategoryService;
import com.wangqinlei.cowims126.service.CowService;
import com.wangqinlei.cowims126.util.AjaxResult;
import com.wangqinlei.cowims126.util.Data;
import com.wangqinlei.cowims126.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author 王勤磊
 */
@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CowService cowService;

    /**
     * 跳转种类页面
     * @return
     */
    @GetMapping("/category_list")
    public String categoryList(){
        return "/category/categoryList";
    }

    /**
     * 异步加载种类列表
     * @param page
     * @param rows
     * @param categoryName
     * @return
     */
    @PostMapping("/getCategoryList")
    @ResponseBody
    public Object getClazzList(@RequestParam(value = "page", defaultValue = "1")Integer page,
                               @RequestParam(value = "rows", defaultValue = "100")Integer rows, String categoryName, String from){
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("pageno",page);
        paramMap.put("pagesize",rows);
        if(!StringUtils.isEmpty(categoryName))  paramMap.put("name",categoryName);
        PageBean<Category> pageBean = categoryService.queryPage(paramMap);
        if(!StringUtils.isEmpty(from) && from.equals("combox")){
            return pageBean.getDatas();
        }else{
            Map<String,Object> result = new HashMap();
            result.put("total",pageBean.getTotalsize());
            result.put("rows",pageBean.getDatas());
            return result;
        }
    }

    /**
     * 添加种类
     * @param category
     * @return
     */
    @PostMapping("/addCategory")
    @ResponseBody
    public AjaxResult addCategory(Category category){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = categoryService.addCategory(category);
            if(count > 0){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("添加成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("添加失败");
        }
        return ajaxResult;
    }

    /**
     * 删除种类
     * @param data
     * @return
     */
    @PostMapping("/deleteCategory")
    @ResponseBody
    public AjaxResult deleteCategory(Data data){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<Integer> ids = data.getIds();
            Iterator<Integer> iterator = ids.iterator();
            while (iterator.hasNext()){  //判断是否存在课程关联学生
                if(!cowService.isCowByCategoryId(iterator.next())){
                    ajaxResult.setSuccess(false);
                    ajaxResult.setMessage("无法删除,存在该种类的牛");
                    return ajaxResult;
                }
            }
            int count = categoryService.deleteCategory(data.getIds());
            if(count > 0){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("删除成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("删除异常！");
        }
        return ajaxResult;
    }

    /**
     * 种类修改
     * @param category
     * @return
     */
    @PostMapping("/editCategory")
    @ResponseBody
    public AjaxResult editCategory(Category category){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = categoryService.editCategory(category);
            if(count > 0){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("修改成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("修改失败");
        }
        return ajaxResult;
    }
}
